package com.police.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SearchRecordManager {
    private DBHelper helper;
    private SQLiteDatabase db;

//    public EventManager(Context context) {
//        helper = new DBHelper(context);
//        //因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);
//        //所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
//        db = helper.getWritableDatabase();
//    }
    
    
    public SearchRecordManager(DBHelper helper) {
        this.helper = helper;
        //因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);
        //所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
        db = helper.getWritableDatabase();
    }
    

    /**
     * 添加搜索记录
     * @param Events
     * 
     */
    public void add(SearchRecord searchRecord) {
        db.beginTransaction();    //开始事务
        try {
//                db.execSQL("INSERT INTO Event VALUES(null, ?, ?, ?)", new Object[]{Event.name, Event.time, Event.userName,Event.phoneNum});
                ContentValues cv=new ContentValues();    //实例化ContentValues对象
                cv.put("name", searchRecord.name);
                cv.put("time", searchRecord.time);
                cv.put("lac", searchRecord.lac);
                cv.put("cid", searchRecord.cid);
                db.insert("SearchRecord", null, cv);         //执行insert()方法
            db.setTransactionSuccessful();    //设置事务成功完成
        } catch (Exception e) {
			// TODO: handle exception
		} finally {
            db.endTransaction();    //结束事务
        }
    }
    /**
     *
     * del event
     * @param eventName
     */
    public void del(String eventName){
        db.delete("Event", "eventName = ?", new String[]{eventName});
    }
    /**
     * query all Events, return list
     * @return List<Event>
     * 
     * 查询搜索记录
     * 
     */
    public List<SearchRecord> query(String whichUser) {
        ArrayList<SearchRecord> searchRecords = new ArrayList<SearchRecord>();
        Cursor c = queryTheCursor();
        while (c.moveToNext()) {
        	SearchRecord searchRecord = new SearchRecord();
        	searchRecord._id = c.getInt(c.getColumnIndex("_id"));
        	searchRecord.name = c.getString(c.getColumnIndex("name"));
        	searchRecord.time = c.getString(c.getColumnIndex("time"));
        	searchRecord.lac = c.getString(c.getColumnIndex("lac"));
        	searchRecord.cid = c.getString(c.getColumnIndex("cid"));
            /**
             * 根据查找某个用户的名字，来取出该用户所办理的事件
             */
            if (searchRecord.name.equals(whichUser)) {
            	searchRecords.add(searchRecord);
            }
        }
        c.close();
        return searchRecords;
    }

    /**
     * 查询当前登录用户（非管理员）的事件
     * @return
     */
//    public List<Event> queryPresentUserEvent(String adminUserName) {
//        ArrayList<Event> Events = new ArrayList<Event>();
//        Cursor c = queryTheCursor();
//        while (c.moveToNext()) {
//            Event Event = new Event();
//            Event._id = c.getInt(c.getColumnIndex("_id"));
//            Event.adminName = c.getString(c.getColumnIndex("adminName"));
//            Event.name = c.getString(c.getColumnIndex("name"));
//            Event.time = c.getString(c.getColumnIndex("time"));
//            Event.userName = c.getString(c.getColumnIndex("userName"));
//            Event.phoneNum = c.getString(c.getColumnIndex("phoneNum"));
//            if (Event.adminName.equals(adminUserName)) {
//            	Events.add(Event);
//            }
//        }
//        c.close();
//        return Events;
//    }
    
    
    
    /**
     * query all Events, return cursor
     *
     * @return Cursor
     */
    public Cursor queryTheCursor() {
        Cursor c = db.rawQuery("SELECT * FROM SearchRecord", null);
        return c;
    }

    /**
     * close database
     */
    public void closeDB() {
        db.close();
    }
}
